{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get every player stats for a given season"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.8.0.1'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import goldsberry\n",
    "import pandas as pd\n",
    "import os.path\n",
    "import csv\n",
    "from goldsberry.team import *\n",
    "pd.set_option(\"display.max_columns\",50) # Change Pandas Display Options\n",
    "goldsberry.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define method that writes data into a csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def write_player_game_log_csv(team_file, player_game_logs):    \n",
    "    with open(team_file, 'w') as csvfile:\n",
    "        fieldnames = ['AST','BLK','DREB','FG3A','FG3M','FG3_PCT','FGA','FGM', 'FG_PCT',\n",
    "                      'FTA', 'FTM', 'FT_PCT', 'GAME_DATE', 'Game_ID', 'LEAGUE_ID', 'MATCHUP', 'MIN', 'OREB', \n",
    "                      'PF', 'PLUS_MINUS', 'PLAYER_ID', 'PTS', 'REB', 'SEASON_ID', 'STL',\n",
    "                      'WL','TOV']\n",
    "        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
    "\n",
    "        writer.writeheader()        \n",
    "        for team_row in player_game_logs.logs():\n",
    "            writer.writerow({'AST' : team_row['AST'],\n",
    "                              'BLK' : team_row['BLK'],\n",
    "                              'DREB' : team_row['DREB'],\n",
    "                              'FG3A' : team_row['FG3A'],\n",
    "                              'FG3M' : team_row['FG3M'],\n",
    "                              'FG3_PCT' : team_row['FG3_PCT'],\n",
    "                              'FGA' : team_row['FGA'],\n",
    "                              'FGM' : team_row['FGM'],\n",
    "                              'FG_PCT' : team_row['FG_PCT'],\n",
    "                              'FTA' : team_row['FTA'],\n",
    "                              'FTM' : team_row['FTM'],\n",
    "                              'FT_PCT' : team_row['FT_PCT'],\n",
    "                              'GAME_DATE' : team_row['GAME_DATE'],\n",
    "                              'Game_ID' : team_row['Game_ID'],\n",
    "                              'MATCHUP' : team_row['MATCHUP'],\n",
    "                              'MIN' : team_row['MIN'],\n",
    "                              'OREB' : team_row['OREB'],\n",
    "                              'PF' : team_row['PF'],\n",
    "                              'PLUS_MINUS' : team_row['PLUS_MINUS'],\n",
    "                              'PTS' : team_row['PTS'],\n",
    "                              'PLAYER_ID' : team_row['Player_ID'],\n",
    "                              'REB' : team_row['REB'],\n",
    "                              'SEASON_ID' : team_row['SEASON_ID'],\n",
    "                              'STL' : team_row['STL'],\n",
    "                              'TOV' : team_row['TOV'],\n",
    "                              'WL': team_row['WL']})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get player list and game logs for each player"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_player_stats(season='2015'):\n",
    "    player_list = goldsberry.PlayerList()\n",
    "    \n",
    "    for player in player_list.players():\n",
    "        player_name = player['PLAYERCODE']\n",
    "        if player_name != None:\n",
    "            csv_name = \"%s_%s.csv\" %(season, player_name)\n",
    "            player_code = player['PERSON_ID']\n",
    "            print csv_name, os.path.isfile(csv_name)\n",
    "\n",
    "            if (os.path.isfile(csv_name) == False):\n",
    "                player_game_logs = goldsberry.player.game_logs(player_code)\n",
    "                write_player_game_log_csv(player_csv, player_game_logs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "get_player_stats()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
